<template>
  <div class="header">
    <div class="left">
      <div class="logo"></div>
      <span class="title">{{loginFlag ? "权限配置平台" : '欢迎登录'}}</span>
      <span class="tips" v-if="!loginFlag">权限配置平台</span>
    </div>
    <div class="right" v-if="!loginFlag">
      <div class="help-center">

      </div>
      <div class="tel">

        <span></span>
      </div>
    </div>
    <div class="right" v-else>
      <div class="avatar_box">
        <!-- <a-avatar :src="userInfo.avatar" class="avatar" @click="logout"/>
        <div class="logout">登出</div> -->
        <a-popconfirm placement="bottomRight" ok-text="确定" cancel-text="取消" @confirm="logout">
          <template slot="title">
            <p>是否退出登录</p>
          </template>
          <a-avatar :src="userInfo.avatar" class="avatar"/>
        </a-popconfirm>
      </div>
      <div class="user_name">
        {{userInfo.name}}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      loginFlag: false,
      userInfo: {}
    };
  },
  created() {
    console.log('created', this);
    this.changeLoginFlag();
  },
  methods: {
    changeLoginFlag() {
      if (this.$route.path === '/login') {
        this.loginFlag = false;
      } else {
        this.loginFlag = true;
        this.userInfo = JSON.parse(localStorage.getItem('userInfo'));
      }
    },
    logout() {
      this.$api.login.logout()
        .then(res => {
          console.log('res', res);
          localStorage.removeItem('userInfo');
          localStorage.removeItem('token');
          this.$router.push('/login');
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  watch: {
    $route() {
      console.log('路由变化', this);
      this.changeLoginFlag();
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="less">
.header{
  width: 100%;
  height: 64px;
  background: #E9ECF2;
  box-shadow: 0px 2px 8px 0px rgba(6, 33, 85, 0.2);
  box-sizing: border-box;
  padding: 12px 52px;
  display: flex;
  justify-content: space-between;
  .left{
    display: flex;
    align-items: center;
    .logo{
      width: 40px;
      height: 40px;
      background: #FFFFFF;
      border-radius: 4px;
      border: 1px solid #FFFFFF;
    }
    .title{
      margin-left: 16px;
      font-size: 18px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #333333;
    }
    .tips{
      margin-left: 20px;
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 500;
      color: #666666;
    }
  }
  .right{
    display: flex;
    align-items: center;
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #666666;
    .tel{
      margin-left: 56px;
      span{
        margin-left: 20px;
      }
    }
    .avatar_box{
      position: relative;
      .logout{
        position: absolute;
      }
      .avatar{
        width: 32px;
        height: 32px;
        margin-right: 8px;
        cursor: pointer;
      }
    }
    .user_name{
      font-size: 14px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #666666;
    }
  }
}
</style>
